package org.myy.learn.hot.链表.反转链表;

public class Solution {
    public static ListNode reverseList(ListNode head) {
        ListNode pre = null, cur = head;
        while (cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;

        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode head = buildNode(new int[]{1,2});
        printList(head);
        ListNode reverse = reverseList(head);
        printList(reverse);
    }
    /**
     * 生成链表
     * @param arr
     * @return
     */
    public static ListNode buildNode(int[] arr){
        if(arr != null && arr.length == 0){return null;}
        ListNode head = new ListNode(arr[0]), cur = head;
        for (int i=1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
        return head;
    }
    public static void printList(ListNode head) {
        while (head != null) {
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        System.out.println("null");
    }
}
